/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml.timgiayto.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import xml.timgiayto.binding.info.Informations;

/**
 *
 * @author mrnghiep
 */
public class XMLTools {

    /**
     * Parse File to DOM.
     * @param fileXMLPatch
     * @return Document
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static Document parseFileToDom(String fileXMLPatch)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        File file = new File(fileXMLPatch);
        Document doc = db.parse(file);
        return doc;
    }

    /**
     * Create XPath.
     * @return
     */
    public static XPath createXPath() {
        XPathFactory xpf = XPathFactory.newInstance();
        XPath xpath = xpf.newXPath();
        return xpath;
    }

    /**
     * MarshallXML from Database.
     * @param informations
     * @param fileName
     */
    public static void marshallXML(Object object, String fileName) {
        try {
            JAXBContext jaxb = JAXBContext.newInstance(object.getClass());
            Marshaller mar = jaxb.createMarshaller();
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.marshal(object, new File(fileName));
        } catch (Exception e) {
            Logger.getLogger(XMLTools.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    /**
     * Transform DOM to File.
     * @param node
     * @param filePath
     * @throws TransformerConfigurationException
     * @throws TransformerException
     */
    public static void transformDOMToFile(Node node, String filePath)
            throws TransformerConfigurationException, TransformerException {
        TransformerFactory tff = TransformerFactory.newInstance();
        Transformer trans = tff.newTransformer();
        Source src = new DOMSource(node);
        File file = new File(filePath);
        Result result = new StreamResult(file);
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.transform(src, result);
    }

    public static void methodTrAX(String xslPath, String xmlPath, String output) {
        try {
            TransformerFactory tff = TransformerFactory.newInstance();
            StreamSource xsltFile = new StreamSource(xslPath);
            
            Transformer trans = tff.newTransformer(xsltFile);
            StreamSource xmlFile = new StreamSource(xmlPath);
            
            StreamResult htmlFile = new StreamResult(new FileOutputStream(output));
            
            trans.transform(xmlFile, htmlFile);
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
